<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');

class Goals extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		// check if user logged in
		if(!$this->ion_auth->logged_in())
		{
			redirect('auth/login');
		}
		$this->load->model('user/user_model');
		$this->load->model('user/goals_model');
	}

	function index()
	{
		$meta['page_title'] = "My Goals";
		$data['my_goals'] = $this->goals_model->get_my_goals();
		$this->load->view('common/header', $meta);
		$this->load->view('user/goals',$data);
		$this->load->view('common/footer');
	}

	function result($id)
	{
		$meta['page_title'] = "My Goals";
		$data['goal'] = $this->goals_model->get_goal_details($id);
		$data['results'] = $this->goals_model->get_results($data['goal']['id']);
		$data['notes'] = $this->goals_model->get_note_by_id($data['goal']['id']);
		$data['my_goals'] = $this->goals_model->get_my_goals();
		$this->load->view('common/header', $meta);
		$this->load->view('user/results',$data);
		$this->load->view('common/footer');
	}

	function create_result($goal_id)
	{

		// $this->input->post('priority');
		$insert = array(
			'goal_id'    => $goal_id,
			'desc'       =>  $this->input->post('result'),
			'priority'   =>$this->input->post('priority'),
			'is_achieved'=> 0,
			'is_heading' => 0,
		);

		// print_r($insert);exit;
		$this->goals_model->create_result($insert);
		$this->session->set_flashdata('result_message', 'Result delted successfully');
		redirect('user/goals/result/'.$goal_id,'refresh');
	}

	function edit_result($id)
	{
		$data['results'] = $this->goals_model->edit_result($id);
		$this->load->view('user/edit-result',$data);
	}


	function update_result()
	{
		$update = array(
			'desc'    => $this->input->post('desc'),
			'priority'=>$this->input->post('priority')
		);

		$id        = $this->input->post('id');
		$result_id = $this->input->post('goal_id');
		$this->session->set_flashdata('result_message', '<i class="icon-ok-sign"></i>  Result has been successfully Update');
		$this->goals_model->update_result($update,$id);


		redirect('user/goals/results/'.$result_id,'refresh');
	}

	function delete_result($goal_id,$id)
	{

		$this->goals_model->delete_result($id);
		redirect('user/goals/result/'.$goal_id,'refresh');
	}

	/* notes function starts here */
	function add_note()
	{
		//echo $_REQUEST['goal_id'];exit;
		$insert = array(
			'user_id'=> $this->session->userdata('user_id'),
			'goal_id'=>	$_REQUEST['goal_id'],
			'note'   =>	nl2br($_REQUEST['note']),
			'date'   => date('Y-m-d')
		);
		//print_r($insert);exit;
		$this->goals_model->add_note($insert);
		$this->session->set_flashdata('note_message', 'Note succesfully added');
	}

	function delete_note($note_id,$goal_id)
	{
		$this->goals_model->delete_note($note_id);
		$this->session->set_flashdata('note_message', 'Note delted successfully');
		redirect('user/goals/result/'.$goal_id,'refresh');
	}
	/**
	*
	* @param undefined $goal_id
	* subheading function starts here
	*/
	function create_result_subheading($goal_id)
	{
		// $this->input->post('priority');
		$insert = array(
			'goal_id'    => $goal_id,
			'desc'       =>  $this->input->post('subheading'),
			'priority'   =>0,
			'is_achieved'=> 0,
			'is_heading' => 1,
		);
		//print_r($insert);
		$this->goals_model->create_result_subheading($insert);
		$this->session->set_flashdata('result_message', '<i class="icon-ok-sign"></i> Result Subheading has been created successfully');
		redirect('user/goals/result/'.$goal_id,'refresh');
	}

	function delete_subheading($id,$goal_id)
	{
		$this->goals_model->delete_subheading($id);
		$this->session->set_flashdata('result_message', '<i class="icon-ok-sign"></i> Result Subheading has been deleted successfully');
		redirect('user/goals/result/'.$goal_id,'refresh');

	}
}

?>